/*!
 * Page: pages/aboutus/Aboutus
 * License: MIT
 * Created: 2019-10-15 17:02
 */

import { siteApi } from '../api/site'
import { userApi } from '/api/user'
const REG_BODY = /<body[^>]*>([\s\S]*)<\/body>/
Page({
  data: {
    content: '',
    loading: false,
  },
  onLoad(query?: any) {
    switch (query.type) {
      case 'vip_protrol':
        wx.setNavigationBarTitle({ title: '会员协议' })
        break
      case 'user_agreement':
        wx.setNavigationBarTitle({ title: '用户协议' })
        break
      case 'privacy_agreement':
        wx.setNavigationBarTitle({ title: '隐私政策' })
        break
      default:
    }

    this.getAboutus(query.type)
  },
  getAboutus(type: any) {
    const {
      data: { loading },
    } = this
    if (!loading) {
      this.setData({ loading: true })
      let a
      switch (type) {
        case 'vip_protrol':
          a = userApi.vipProtocol()
          break
        case 'user_agreement':
          a = userApi.showTerm(type)
          break
        case 'privacy_agreement':
          a = userApi.showTerm(type)
          break
        default:
          // 该方案需要配置业务域名
          // this.setData({
          //   url: '{{SERVER_PROD_API}}' + '/api/v2/aboutus',
          // })
          // return
          a = siteApi.aboutUs()
      }

      a.then((data) => {
        this.setData({
          content: this.getBody(data, type),
          loading: false,
        })
      })
    }
  },
  getBody(content: any, type: any) {
    switch (type) {
      case 'user_agreement':
        this.setData({
          url: content.url,
        })
        content = content.content
        break
      case 'privacy_agreement':
        this.setData({
          url: content.url,
        })
        content = content.content
        break
      default:
    }
    const result = REG_BODY.exec(content)
    if (result && result.length === 2) return result[1]
    return content
  },
})
